﻿using System;
using System.Web;


    /// <summary>
    /// HTTP请求常用方法
    /// </summary>
    public static class URL
    {
        /// <summary>
        /// URL编码
        /// </summary>
        /// <param name="URL"></param>
        /// <returns></returns>
        public static string UrlEncode(string URL)
        {
            if (!string.IsNullOrEmpty(URL))
                return System.Web.HttpUtility.UrlEncode(URL);
            else
                return "";
        }
        /// <summary>
        /// URL解码
        /// </summary>
        /// <param name="URL"></param>
        /// <returns></returns>
        public static string UrlDecode(string URL)
        {
            if (!string.IsNullOrEmpty(URL))
                return System.Web.HttpUtility.UrlDecode(URL);
            else
                return "";
        }
        /// <summary>
        /// HTML编码
        /// </summary>
        /// <param name="URL"></param>
        /// <returns></returns>
        public static string HTMLEncode(string HTML)
        {
            if (!string.IsNullOrEmpty(HTML))
            {
                HTML = System.Web.HttpUtility.HtmlEncode(HTML);
                return HTML;
            }
            else
                return "";
        }
        /// <summary>
        /// HTML解码
        /// </summary>
        /// <param name="URL"></param>
        /// <returns></returns>
        public static string HTMLDecode(string HTML)
        {
            if (!string.IsNullOrEmpty(HTML))
            {
                HTML = System.Web.HttpUtility.HtmlDecode(HTML);

                return HTML;
            }
            else
                return "";
        }

        public static string GetUrl()
        {
            string url = System.Web.HttpContext.Current.Request.Url.ToString();
            if (url.Split('/').Length >= 3)
            {
                url = url.Split('/')[0] + "/" + url.Split('/')[1] + "/" + url.Split('/')[2];
            }
            return url;
        }

        public static string GetUrl(string url)
        {
            if (url.Split('/').Length >= 3)
            {
                url = url.Split('/')[0] + "/" + url.Split('/')[1] + "/" + url.Split('/')[2];
            }
            return url;
        }

        public static string UrlFormat(string url)
        {
            if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(url.Trim()))
            {
                return "";
            }
            else
            {
                return "http://" + url.ToLower().Replace("http://", "");
            }
        }

        public static string RootFormat(string rootPath)
        {
            if (string.IsNullOrEmpty(rootPath) || string.IsNullOrEmpty(rootPath.Trim()))
            {
                return "";
            }
            else
            {
                if (rootPath.Split('/')[rootPath.Split('/').Length - 1] == "")
                {
                    return rootPath.Remove(rootPath.Length - 1, 1);
                }
                else
                {
                    return rootPath;
                }
            }
        }
    }